#include "epollServer.hpp"
#include "err.hpp"
#include <memory>
#include <unordered_map>

using namespace std;
using namespace epoll_ns;

static void usage(std::string proc)
{
    std::cerr << "Usage:\n\t" << proc << " port"
              << "\n\n";
}

std::string transaction(const std::string &request)
{
    return request;
}

std::string translate(const std::string &request)
{
    if (request.empty())
    {
        return "请输入有效字符串";
    }
    string tmp = request;
    for (char &c : tmp)
    {
        if (isupper(c))
            c += 32;
    }
    while (tmp.size() > 0 && !isalnum(tmp.back()))
    {
        tmp.pop_back();
    }
    std::unordered_map<string, string> letterMp = {
        {"hello", "你好"}, {"left", "左边；离开"}, {"right", "权利：正确的"}, {"world", "世界"}};
    if (letterMp.find(tmp) != letterMp.end())
    {
        return letterMp[tmp];
    }
    else
    {
        return "暂未收录该词";
    }
}

// ./select_server 8081
int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        usage(argv[0]);
        exit(USAGE_ERR);
    }

    // unique_ptr<EpollServer> svr(new EpollServer(transaction, atoi(argv[1])));
    unique_ptr<EpollServer> svr(new EpollServer(translate, atoi(argv[1])));

    svr->initServer();

    svr->start();

    return 0;
}